import { Query } from '@cubejs-client/core'
import { QueryItem } from '@/api/lix.ts'
import utils from 'xe-utils'
import { formatDate } from '@/components/charts/base.ts'

export function useQuery(items: QueryItem[], extend?: string | null) {
  const queryItems = ref<{ name: string; tooltip: string }[]>([])

  const getGranularity = (query: Query) => {
    let granularity = undefined

    if (query.timeDimensions) {
      query.timeDimensions.forEach((time) => {
        granularity = time.granularity
      })
    }
    return granularity
  }

  const setQuery = (params: Query) => {
    const granularity = getGranularity(params)

    if (params.dimensions) {
      params.dimensions.forEach((dimension) => {
        const target = items.find((item) => item.name == dimension)
        queryItems.value.push({
          name: target?.title || dimension,
          tooltip: `维度(${target?.title || dimension})`,
        })
      })
    }

    if (params.measures) {
      params.measures.forEach((measure) => {
        const target = items.find((item) => item.name == measure)
        queryItems.value.push({
          name: target?.title || measure,
          tooltip: `指标(${target?.title || measure})`,
        })
      })
    }

    if (params.timeDimensions) {
      let times = params.timeDimensions

      times.forEach((time) => {
        const dim = time.dimension

        const target = items.find((item) => item.name == dim)
        if (time.dateRange) {
          queryItems.value.push({
            name: utils.isArray(time.dateRange)
              ? time.dateRange.map((d) => formatDate(d, granularity)).join(' 至 ')
              : formatDate(time.dateRange, granularity),
            tooltip: `维度(${target.name})：${target?.title}`,
          })
        } else if (time.compareDateRange) {
          let str: string[] = []
          time.compareDateRange.forEach((dateRange: string[] | string, index: number) => {
            if (index == 0) {
              if (utils.isArray(dateRange)) {
                str.push(
                  granularity
                    ? `${formatDate(dateRange[0], granularity)} 至 ${formatDate(dateRange[1], granularity)}`
                    : dateRange.join(' 至 '),
                )
              } else {
                str.push(formatDate(dateRange, granularity))
              }
            }
          })
          queryItems.value.push({
            name: str.join(),
            tooltip: `维度(${target.name})：${target?.title}`,
          })
        }
      })
    }
    if (params.filters) {
      params.filters.forEach((filter) => {
        const target = items.find((item) => item.name == (filter.member || filter.dimension))
        if (target) {
          queryItems.value.push({
            name: utils.isArray(filter.values) ? filter.values.join(',') : filter.values,
            tooltip: `维度(${target.name})：${target?.title}`,
          })
        }
      })
    }

    if (extend) {
      queryItems.value.push({
        name: extend,
        tooltip: '',
      })
    }
  }
  return {
    setQuery,
    queryItems,
  }
}
